package com.bestcem.xm.deliver.convert;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.deliver.controller.v2.param.CreateWechatMpdeliversParam;
import com.bestcem.xm.deliver.controller.v2.vo.*;
import com.bestcem.xm.deliver.entity.*;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;
import org.mapstruct.Named;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author luguitao
 * @version v1.0
 * @date 2021/8/18 17:27
 */
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
public interface DeliverConvertMapper {

    @Named("list2JsonString")
    default String list2JsonString(List<?> list) {
        if (CollectionUtils.isEmpty(list)) {
            return "[]";
        }
        return JSONArray.toJSONString(list);
    }

    @Named("jsonString2List")
    default JSONObject jsonString2List(String value) {
        if (StrUtil.isEmpty(value)) {
            return new JSONObject();
        }
        return JSONObject.parseObject(value);
    }


    DeliverVo deliver2Vo(DeliverDeliver deliver);

    DeliverWechatmpRecordVo wechatRecord2Vo(DeliverWechatmpRecord obj);

    DedicatedDeliverVo dedicate2Vo(DeliverDedicatedDeliver deliver);

    @Mapping(target = "antiDisturTimes", source = "antiDisturTimes", qualifiedByName = "list2JsonString")
    @Mapping(target = "mpTemplateContent", source = "mpTemplateContent", defaultValue = "[]")
    @Mapping(target = "checkSubmit", source = "checkSubmit", defaultValue = "0")
    @Mapping(target = "isDelay", source = "isDelay", defaultValue = "0")
    @Mapping(target = "isCheck", source = "isCheck", defaultValue = "0")
    @Mapping(target = "optionDeliverTime", source = "optionDeliverTime", defaultValue = "{}")
    @Mapping(target = "filterParams", source = "filterParams", defaultValue = "{}")
    @Mapping(target = "noDisturbMemberIdList", source = "noDisturbMemberIdList", qualifiedByName = "list2JsonString")
    @Mapping(target = "mpTtype", source = "mpTtype", defaultValue = "0")
    DeliverWechatmpDeliver wxMpdeliverObj2Entity(CreateWechatMpdeliversParam param);

    DeliverStoreVo store2Vo(DeliverDefaultDeliverStore deliver);

    @Mapping(target = "paramsObj", source = "params", qualifiedByName = "jsonString2List")
    DeliverEmailRecordVo emailRecord2Vo(DeliverEmailRecord obj);

    @Mapping(target = "paramsObj", source = "params", qualifiedByName = "jsonString2List")
    DeliverSmsRecordVo smsRecord2Vo(DeliverSmsRecord obj);
}
